package team1.project2.scribeagent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.scribe.ScribeAppender;

public class ScribeLogAgent {
	static Logger logger = Logger.getLogger("ScribeLogAgent");
	static Properties properties = new Properties();
	boolean sendDataFlag = true;
	
	public ScribeLogAgent(boolean sendDataFlag) {
		this.sendDataFlag = sendDataFlag;
	}

	
	public boolean isSendDataFlag() {
		return sendDataFlag;
	}


	public void setSendDataFlag(boolean sendDataFlag) {
		this.sendDataFlag = sendDataFlag;
	}


	public ScribeLogAgent() {
	}

	static {
		String scribeAgentPropertiesPath = ScribeLogAgent.class
				.getResource("/").getPath()
				+ "../resources/scribeagent.properties";
		System.out.println("scribeAgentPropertiesPath:"
				+ scribeAgentPropertiesPath);
		if (scribeAgentPropertiesPath != null) {
			try {
				properties.load(new FileInputStream(new File(
						scribeAgentPropertiesPath)));
			} catch (FileNotFoundException e) {
				System.out
						.println("Unable to load file, please check properties file path, scribeAgentPropertiesPath="
								+ scribeAgentPropertiesPath);
				e.printStackTrace();
			} catch (IOException e) {
				System.out
						.println("Unable to load file, file is corrupt, please check properties file, scribeAgentPropertiesPath="
								+ scribeAgentPropertiesPath);
				e.printStackTrace();
			}
			System.out.println(properties);

		}

	}

	// write thread which pollse send log every 5 sec
	public static void main(String[] args) {
//		String logType = "vmkernel";
//		String logFileName = "/home/chukwa/Downloads/logs/vmkernel.log";
//		new ScribeLogAgent().sendLogs(logType, logFileName);
		new ScribeLogAgent().sendSysLogsDir();
		new ScribeLogAgent().sendVmKernelLogsDir();
	}

	public void sendVmKernelLogsDir() {
		String logType = properties
				.getProperty(ScribeAgentPropertiesKeys.KERNEL_LOG_TYPE);
		String logFileDir = properties
				.getProperty(ScribeAgentPropertiesKeys.KERNEL_LOG_DIR);

		processDir(logType, logFileDir);
	}

	public void sendSysLogsDir() {
		String logType = properties
				.getProperty(ScribeAgentPropertiesKeys.SYS_LOG_TYPE);
		String logFileDir = properties
				.getProperty(ScribeAgentPropertiesKeys.SYS_LOG_DIR);
		
		processDir(logType, logFileDir);
	}


	public void processDir(String logType, String logFileDir) {
		File folder = new File(logFileDir);
		File[] listOfFiles = folder.listFiles();
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				String logFileName = logFileDir + listOfFiles[i].getName();
				System.out.println("Processing log file=" + logFileName);
				new ScribeLogAgent().sendLogs(logType, logFileName);
			}
		}

	}

	public void sendLogs(String logType, String logFileName) {
		Logger logger = Logger.getRootLogger();
		Appender appender = logger.getAppender("scribe");
		ScribeAppender scribeAppender = (ScribeAppender) appender;

		scribeAppender.setScribe_category(logType);
		getContents(new File(logFileName));

	}

	public String getContents(File aFile) {
		// ...checks on aFile are elided
		StringBuilder contents = new StringBuilder();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(new FileReader(aFile));

			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					// contents.append(line);
					// contents.append(System.getProperty("line.separator"));
					if(this.sendDataFlag){
						logger.info(line);
					}
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return contents.toString();
	}

}
